package com.showde.shoppingmall.business.logic;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.kim.net.NetEntity;
import com.showde.shoppingmall.business.NetCallbackImplBase.Notify;
import com.showde.shoppingmall.business.ProductBusiness;
import com.showde.shoppingmall.db.ProductDBCommand;
import com.showde.shoppingmall.db.ProductDescDBCommand;
import com.showde.shoppingmall.domain.Product;
import com.showde.shoppingmall.domain.ProductDesc;
import com.showde.shoppingmall.domain.SearchResult;
import com.showde.shoppingmall.netentity.ProductEntity.FETCHTYPE;

public final class ProductLogic {

	private static final String TAG = "ProductLogic";

	public static final void fetchProductSummary(final Context context,
			String productId, final UICallbackInterface uiCallbackInterface) {
		ProductBusiness productBusiness = new ProductBusiness(productId,
				new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						JSONObject jsonObject = netEntity.result;
						try {
							if (!"0".equals(jsonObject.getInt("ReturnCode")
									+ "")) {
								uiCallbackInterface.onError("fail::"
										+ netEntity.receiveData);
								return;
							}

							JSONArray jsonArray = jsonObject
									.getJSONArray("List");
							if (jsonArray != null && jsonArray.length() > 0) {
								SearchResult searchResult = null;
								List<SearchResult> searchResults = new ArrayList<SearchResult>();
								ProductDBCommand command = new ProductDBCommand(context);
								for (int i = 0; i < jsonArray.length(); i++) {
									JSONObject arrayItem = jsonArray
											.getJSONObject(i);
									searchResult = new SearchResult();
									searchResult.pID = arrayItem.getInt("ID")
											+ "";
									searchResult.pName = arrayItem
											.getString("NAME");
									searchResult.pDesc = arrayItem
											.getString("TITLE");
									searchResult.pPrice = arrayItem
											.getDouble("PRICE") + "";
									searchResult.pLogo = arrayItem
											.getString("LOGO");
									searchResults.add(searchResult);

									// add to db
									Product product = new Product();
									product.setProductID(arrayItem.getInt("ID")
											+ "");
									product.setProductName(arrayItem
											.getString("NAME"));
									product.setProductTitle(arrayItem
											.getString("TITLE"));
									product.setProductPrice((float) arrayItem
											.getDouble("PRICE"));
									product.setProductLogo(arrayItem
											.getString("LOGO"));
									command.add(product);
								}

								CallbackData data = new CallbackData();
								data.data = searchResults;
								uiCallbackInterface.onSuccess(data);
								return;
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						// Log.d(TAG, "receiveData:" + netEntity.receiveData);
						uiCallbackInterface.onError("fail::"
								+ netEntity.receiveData);
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						uiCallbackInterface.onError(errorMsg);
					}
				}, FETCHTYPE.product_summary);
		productBusiness.doBusiness();
	}

	public static final void fetchProductDetails(final Context context,
			String productId, final UICallbackInterface callbackInterface) {
		ProductBusiness productBusiness = new ProductBusiness(productId,
				new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						JSONObject jsonObject = netEntity.result;
						Log.d(TAG, "receiverData:" + netEntity.receiveData);
						try {
							if (!"0".equals(jsonObject.getInt("ReturnCode")
									+ "")) {
								callbackInterface.onError("fail::"
										+ netEntity.receiveData);
								return;
							}

							JSONObject jsonProductDesc = jsonObject
									.getJSONObject("info");
							if (jsonProductDesc != null) {
								ProductDesc productDesc = new ProductDesc();

								productDesc.setProductDescId(jsonProductDesc
										.getString("ID"));
								productDesc.setProductDescName(jsonProductDesc
										.getString("NAME"));
								productDesc.setProductDescTitle(jsonProductDesc
										.getString("TITLE"));
								productDesc
										.setProductDescPrice((float) jsonProductDesc
												.getDouble("PRICE"));
								productDesc
										.setProductDescMPrice((float) jsonProductDesc
												.getDouble("MPRICE"));
								productDesc
										.setProductDescVIPPrice((float) jsonProductDesc
												.getDouble("VIPPRICE"));
								productDesc.setProductDescNum(jsonProductDesc
										.getInt("NUM"));
								productDesc.setProductDescSizes(jsonProductDesc
										.getString("SIZES"));
								productDesc
										.setProductDescColors(jsonProductDesc
												.getString("COLORS"));
								productDesc.setProductDescLogo(jsonProductDesc
										.getString("LOGO"));
								productDesc.setProductDescDesc(jsonProductDesc
										.getString("DESC"));
								// productDesc.setProductDescPostFee(jsonProductDesc.getString(""));
								// productDesc.setProductDescIsOutOfStack(jsonProductDesc.getString(""));
								productDesc
										.setProductCaregoryId(jsonProductDesc
												.getString("TYPEID"));

								ProductDescDBCommand dbCommand = new ProductDescDBCommand(
										context);
								dbCommand.add(productDesc);
								dbCommand = null;

								CallbackData data = new CallbackData();
								data.data = productDesc;
								callbackInterface.onSuccess(data);
								return;
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						callbackInterface.onError(errorMsg);
					}
				}, FETCHTYPE.product_details);
		productBusiness.doBusiness();
	}

	public static final void fetchProductByCategoryId(final Context context,
			String productCategoryId,
			final UICallbackInterface callbackInterface) {
		ProductBusiness productBusiness = new ProductBusiness(
				productCategoryId, new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						JSONObject jsonObject = netEntity.result;
						try {
							if (!"0".equals(jsonObject.getInt("ReturnCode")
									+ "")) {
								callbackInterface.onError("fail::"
										+ netEntity.receiveData);
								return;
							}

							JSONArray jsonArray = jsonObject
									.getJSONArray("List");
							if (jsonArray != null && jsonArray.length() > 0) {
								Product product = null;
								List<Product> products = new ArrayList<Product>();
								for (int i = 0; i < jsonArray.length(); i++) {
									JSONObject arrayItem = jsonArray
											.getJSONObject(i);
									product = new Product();
									product.setProductID(arrayItem.getInt("ID")
											+ "");
									product.setProductName(arrayItem
											.getString("NAME"));
									product.setProductTitle(arrayItem
											.getString("TITLE"));
									product.setProductLogo(arrayItem
											.getString("LOGO"));
									product.setProductCategoryID(arrayItem
											.getInt("TYPEID") + "");
									product.setProductPrice((float) arrayItem
											.getDouble("PRICE"));
									products.add(product);
								}
								ProductDBCommand productDBCommand = new ProductDBCommand(
										context);
								productDBCommand.add(products);
								productDBCommand = null;
								CallbackData data = new CallbackData();
								data.data = products;
								callbackInterface.onSuccess(data);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						Log.d(TAG, "receiveData:" + netEntity.receiveData);
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						Log.d(TAG, "errorMsg:" + errorMsg);
						callbackInterface.onError(errorMsg);
					}
				});
		productBusiness.doBusiness();
	}

}
